package cn.exrick.xboot.modules.captchaLogin.controller;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.base.entity.User;
import cn.exrick.xboot.modules.base.entity.UserRole;
import cn.exrick.xboot.modules.base.service.UserService;
import cn.exrick.xboot.modules.base.service.mybatis.IUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author    : LiYuanzhuo
 * description: 验证码登录方式
 * create time: 2019/9/18 13:59
 */
@Slf4j
@RestController
public class SsoLoginValidate {

    @Autowired
    private UserService userService;
    @Autowired
    private IUserRoleService iUserRoleService;

    /*@Autowired
    @Qualifier(value = "IUserRoleServiceImpl")
    private IUserRoleService iUserRoleService;*/

    @RequestMapping(value = "/ssoLogin", method = RequestMethod.POST)
    public Result<Object> ssoLogin(@RequestParam String username, @RequestParam String password) throws Exception {

        //在本地查询，是否为本地用户
        User user = userService.findByUsername(username);
        if (user != null) {
            if (!new BCryptPasswordEncoder().matches(password, user.getPassword())) {
                return new ResultUtil<Object>().setErrorMsg("用户名或密码错误");
            }
            if (user.getStatus().equals(CommonConstant.USER_STATUS_LOCK)){
                return new ResultUtil<Object>().setErrorMsg("该用户已经被禁用！请联系管理员！");
            }
        }else{
            return new ResultUtil<Object>().setErrorMsg("用户名或密码错误");
        }
        /**
         * njp 20190925 add
         * 添加三权分立开发中的超级管理员与审批员账号的判断 begin
         */
        AdminFlag  adminFlag=new AdminFlag();
        UserRole userRole = new UserRole();
        userRole.setRoleId(CommonConstant.APPROVAL_ROLRID);
        userRole.setUserId(userService.findByUsername(username).getId());
        if("admin".equals(username)){
            //查询是否有审批员
            if(iUserRoleService.findUserRoleByRoleId(userRole).size()>0){
                log.info("超级管理员账号（并系统中存在审批员）");
                adminFlag.isRole="adminY";
            }else{
                log.info("超级管理员账号（并系统中不存在审批员需要注册）");
                adminFlag.isRole="adminN";
            }
        //}else if(new SsoLoginValidate().getApprovalRole(iUserRoleService.findUserRoleByUserId(userRole))){//检测账户是否是审批员的角色
        }else if(iUserRoleService.getApprovalRole(user)){//检测账户是否是审批员的角色(重新封装)
            log.info("该账户"+username+"是审批员");
            adminFlag.isRole="assessor";
        }

        return new ResultUtil<Object>().setData(adminFlag,"验证通过");
        /**
         * njp 20190925 add
         * 添加三权分立开发中的超级管理员与审批员账号的判断 end
         */
        //return new ResultUtil<Object>().setSuccessMsg("验证通过");
    }

    /**
     *检测账户角色中有没有审批员角色
     */
    public   boolean getApprovalRole(List<UserRole> list){
        try{
            //List<UserRole> list = iUserRoleService.findUserRoleByUserId(userRole);
            //System.out.println(list.size()+"@@@@@@");
            for (UserRole role : list) {
                if(CommonConstant.APPROVAL_ROLRID.equals(role.getRoleId())){
                    log.info("该用户是审批员");
                    return true;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return false;
    }

    class AdminFlag{
        public String isRole="other";
    }
}
